package com.wxproject.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author wangkai 用户管理
 */
public class UserData {

	private Map<String, Object> tag;// 标签

	private String tagid;// 标签id
	private String next_openid;// 第一个拉取的OPENID，不填默认从头开始拉取

	private List<String> openid_list;// 粉丝列表
	private String openid;// 用户id

	private String remark;// 新的备注名，长度必须小于30字符 

	private List<Map<String, Object>> user_list;// 用户列表

	private String begin_openid;// 黑名单用户openid

	public UserData() {
		this.tag = new HashMap<String, Object>();
		this.openid_list = new ArrayList<String>();
		this.user_list = new ArrayList<Map<String, Object>>();
	}

	public String getRemark() {
		return remark;
	}

	public UserData setRemark(String remark) {
		this.remark = remark;
		return this;
	}

	public List<Map<String, Object>> getUser_list() {
		return user_list;
	}

	public UserData setUser_list(String... openid) {
		for (int i = 0; i < openid.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("openid", openid[i]);
			map.put("lang", "zh_CN");
			this.user_list.add(map);
		}
		return this;
	}

	public static UserData NOW() {

		return new UserData();
	}

	public Map<String, Object> getTag() {
		return tag;
	}

	public UserData setTag(String key, String value) {
		this.tag.put(key, value);
		return this;
	}

	public String getTagid() {
		return tagid;
	}

	public UserData setTagid(String tagid) {
		this.tagid = tagid;
		return this;
	}

	public String getNext_openid() {
		return next_openid;
	}

	public UserData setNext_openid(String next_openid) {
		this.next_openid = next_openid;
		return this;
	}

	public List<String> getOpenid_list() {
		return openid_list;
	}

	public UserData setOpenid_list(String... openid_id) {
		for (int i = 0; i < openid_id.length; i++) {
			this.openid_list.add(openid_id[i]);
		}
		return this;
	}

	public String getOpenid() {
		return openid;
	}

	public UserData setOpenid(String openid) {
		this.openid = openid;
		return this;
	}

	public String getBegin_openid() {
		return begin_openid;
	}

	public void setBegin_openid(String begin_openid) {
		this.begin_openid = begin_openid;
	}

	public String build() {
		JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(this));
		Set<String> set = jsonObject.keySet();
		List<String> ids = new ArrayList<String>();
		for (String string : set) {
			String value = jsonObject.getString(string);
			if (value.startsWith("{}")) {
				ids.add(string);
			}
			if (string.equals("openid_list") || string.equals("user_list")) {
				JSONArray arr = JSON.parseArray(value);
				if (!(arr.size() > 0)) {
					ids.add(string);
				}
			}
		}
		for (String key : ids) {
			jsonObject.remove(key);
		}
		return jsonObject.toJSONString();
	}

}
